package j2.shopmail;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import j2.shopmail.ui.home.HomeFragment;

import java.util.ArrayList;

public class Goods
{
    public  static ArrayList<Goods> NowGoods=new ArrayList<>();
    
   public String ID="空";
    public Bitmap Image;
    public String Name="加载中";
    public double Price=0;
    public ImageView imgTarget;
    public boolean isSetImage=false;
    AsycHttpAction<String,Bitmap> action= new AsycHttpAction<String, Bitmap>() {
        @Override
        public Bitmap BackGroundAction(String... params)
        {
            byte[] data = ImageHttpHelper.getImage(params[0]);
            //把二进制图片转成位图
            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            return bitmap;
        }
        
        @Override
        public void ProgressUpdateAction(View UI, Double... values)
        {
        
        }
        
        @Override
        public void ResultFunction(Bitmap result)
        {
            Image=result;
            if(imgTarget!=null)
            {
                imgTarget.setImageBitmap(Image);
                isSetImage=true;
            }

        }
        
        @Override
        public void PreDoFunction()
        {
        
        }
    };
    AsycHttpAction<Integer,RawGood> goodsaction=new AsycHttpAction<Integer,RawGood>() {
        @Override
        public RawGood BackGroundAction(Integer... params)
        {
            RawGood good;
            try
            {
                String res=  HttpHelper.Get(ServerHelper.MakeGoodInfoStr(params[0])).trim();

                JSONObject jsonObject = JSON.parseObject(res);
                good= JSON.parseObject(res, RawGood.class);
            }catch(Exception ex)
            {
                System.out.print(ex.getMessage());
                good=new RawGood();
            }

            return good;
        }
    
        @Override
        public void ProgressUpdateAction(View UI, Double... values)
        {
        
        }
    
        @Override
        public void ResultFunction(RawGood result)
        {
        ID=String.valueOf( result.GoodID);
        Price=result.Price;
        Name=result.GoodName;
            HomeFragment.AddItem(Goods.this);
        setImage(result.GoodIamge);
        }
    
        @Override
        public void PreDoFunction()
        {
        
        }
    };
    protected void setImage(String minmap)
    {
        AsycHttp<String,Bitmap> http = new AsycHttp<>();
        http.setBackgroundAction(action);
        http.execute(ServerHelper.MakeGetImageStr(minmap));
    }
    public Goods(String id,String name,double price,Bitmap minmap,ImageView img)
    {
        imgTarget=img;
        ID=id;
        Name=name;
        Price=price;
        Image=minmap;
    }
    public Goods()
    {
    
    }
    public Goods(int id)
    {
        ID=String.valueOf(id);
        AsycHttp<Integer,RawGood> http = new AsycHttp<>();
        http.setBackgroundAction(goodsaction);
        http.execute(id);
    }
    
    public Goods(String id,String name,double price,String minmap,ImageView img)
    {
        imgTarget=img;
        ID=id;
        Name=name;
        Price=price;

    }
}
